package controllers

import (
	"actile/cache"
	"actile/models"
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"time"
)

type ActileController struct {
	beego.Controller
}

func (c *ActileController) Create() {
	var err error

	title := c.GetString("title")
	name := c.GetString("name")
	people := c.GetString("people")
	comment, _ := c.GetInt("comment")
	status := c.GetString("status")
	begin := c.GetString("begin")

	if title == "" || name == "" || people == "" || comment == 0 || status == "" || begin == "" {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "参数不能为空",
		}
		c.ServeJSON()
		return
	}

	actile := models.Actile{
		Title:   title,
		Name:    name,
		People:  people,
		Comment: comment,
		Status:  status,
		Begin:   begin,
	}

	if err = actile.Create(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "文章添加失败",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("actile_createID_%d", actile.ID)
	jsonData, _ := json.Marshal(&actile)
	cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24)

	c.Data["json"] = map[string]interface{}{
		"code":    200,
		"message": "文章添加成功",
	}
	c.ServeJSON()
}
func (c *ActileController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")

	var err error
	var actile models.Actile
	var list []models.Actile

	key := "actile_list"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()

	if cacheData == "" {
		list, _ := actile.List()
		if len(list) != 0 {
			jsonData, _ := json.Marshal(&list)
			cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24)
		}
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "文章列表展示失败",
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"code":    200,
		"message": "文章列表展示成功",
		"data":    list,
	}
	c.ServeJSON()
}

func (c *ActileController) Update() {
	var err error

	id, _ := c.GetInt("id")
	title := c.GetString("title")
	name := c.GetString("name")
	people := c.GetString("people")
	comment, _ := c.GetInt("comment")
	status := c.GetString("status")
	begin := c.GetString("begin")

	var actile models.Actile
	actile.ID = uint(id)
	actile.Title = title
	actile.Name = name
	actile.People = people
	actile.Comment = comment
	actile.Status = status
	actile.Begin = begin

	if err = actile.GetActilrById(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "系统异常",
		}
		c.ServeJSON()
		return
	}

	if actile.ID == 0 {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "文章ID有误",
		}
		c.ServeJSON()
		return
	}

	if err = actile.Update(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "文章修改失败",
		}
		c.ServeJSON()
		return
	}

	actile.GetActilrById()

	key := fmt.Sprintf("actile_updateID_%d", actile.ID)
	jsonData, _ := json.Marshal(&actile)
	cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24)

	c.Data["json"] = map[string]interface{}{
		"code":    500,
		"message": "文章修改成功",
	}
	c.ServeJSON()
}
func (c *ActileController) Del() {
	var err error
	id, _ := c.GetInt("id")
	var actile models.Actile
	actile.ID = uint(id)

	if err = actile.GetActilrById(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "系统异常",
		}
		c.ServeJSON()
		return
	}

	if err = actile.Del(); err != nil {
		if actile.ID == 0 {
			c.Data["json"] = map[string]interface{}{
				"code":    500,
				"message": "文章删除失败",
			}
			c.ServeJSON()
			return
		}
	}

	key := fmt.Sprintf("actile_updateID_%d", actile.ID)
	cache.Rdb.Del(cache.Ctx, key)

	c.Data["json"] = map[string]interface{}{
		"code":    500,
		"message": "文章删除成功",
	}
	c.ServeJSON()

}
